package com.huatai.exchange.impl;

import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.CloseUtils;
import com.faneui.common.utils.FileUtils;
import com.faneui.common.utils.ListUtils;
import com.faneui.dal.DBUtils;
import com.faneui.dbf.model.DBFObject;
import com.faneui.dbf.utils.DBFUtils;
import com.google.common.collect.Lists;
import com.huatai.exchange.dal.dao.ExcFileInfoDAO;
import com.huatai.exchange.dal.model.ExcFileInfo;
import com.huatai.exchange.facade.FileSourceContents;
import com.huatai.exchange.facade.enums.FileSourceTypeEnum;
import com.huatai.exchange.facade.enums.FileTypeEnum;
import com.huatai.exchange.integration.base.service.FileOperateService;
import com.huatai.exchange.service.FileCreateCenterService;
import com.linuxense.javadbf.DBFField;
import groovy.io.FileType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class FileCreateCenterServiceImpl implements FileCreateCenterService {

    @Autowired
    private ExcFileInfoDAO fileInfoDAO;

    @Autowired
    private FileOperateService fileOperateService;

    @Value("${tmp_file_path}")
    private String tmpFile;

    @Override
    public void checkCreateAndDownloadFile(String fileId, Map<String, Object> resvMap) throws BusinessException {
        ExcFileInfo info = fileInfoDAO.queryById(fileId);
        checkFileInfo(info,resvMap);
    }

    @Override
    public void downloadFile(String filePath, String fileId, Map<String, Object> resvMap) throws BusinessException {
        File f = new File(filePath);
        f.deleteOnExit();
        OutputStream os = null;
        try {
            os = new FileOutputStream(f);
            downloadFile(os, fileId, resvMap);
        } catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        } finally {
            CloseUtils.safeClose(os);
        }
    }

    @Override
    public void downloadFile(OutputStream os, String fileId, Map<String, Object> resvMap) throws BusinessException {
        ExcFileInfo info = fileInfoDAO.queryById(fileId);
        // sql 查询
        if (FileSourceTypeEnum.sql.toString().equals(info.getFileSourceType())) {
            checkFileInfo(info, resvMap);
//            downloadSqlCreateFile(os, info, resvMap);
        }

    }

//    public void downloadSqlCreateFile(OutputStream os, ExcFileInfo info, Map<String, Object> resvMap) throws BusinessException {
//        String sql = (String) resvMap.get(FileSourceContents.SQL_CONTENT);
//        String jdbcUrl = (String) resvMap.get(FileSourceContents.SQL_JDBC_URL);
//        String jdbcUser = (String) resvMap.get(FileSourceContents.SQL_JDBC_USER);
//        String jdbcPassword = (String) resvMap.get(FileSourceContents.SQL_JDBC_PASSWORD);
//        Connection conn = null;
//        try {
//            conn = DBUtils.getConnection(jdbcUrl, jdbcUser, jdbcPassword);
//            List<Map<String, Object>> result = DBUtils.executeQuery(conn, sql);
//            String fileType = (String) resvMap.get(FileSourceContents.FILE_TYPE);
//            String fileTemplate = (String) resvMap.get(FileSourceContents.FILE_TEMPLATE);
//            if (FileTypeEnum.DBF.equals(fileType)) {
//                // 如果是空文件
//                if (ListUtils.isEmpty(result)) {
//                    fileOperateService.downloadFile(os, fileTemplate);
//                } else {
//                    String tmpFilePath = tmpFile + UUID.randomUUID().toString();
//                    fileOperateService.downloadFile(new File(tmpFilePath), fileTemplate);
//                    DBFField[] fields = DBFUtils.getDBFields(tmpFilePath);
//                    // 下载 模板文件
//                    List<Object[]> objectsList = Lists.newArrayList();
//                    for (Map<String, Object> dataMap : result) {
//                        List<Object> objects = Lists.newArrayList();
//                        for (DBFField field : fields) {
//                            objects.add(dataMap.get(field.getName().toUpperCase()));
//                        }
//                        objectsList.add(objects.toArray());
//                    }
//                    DBFUtils.addDBFRecord(os, fields, objectsList);
//                }
//            }
//        } catch (Exception ex) {
//            BusinessException.throwBusinessException(ex);
//        } finally {
//            DBUtils.closeAll(conn);
//        }
//    }

    private void checkFileInfo(ExcFileInfo info, Map<String, Object> resvMap) {
        if (FileSourceTypeEnum.sql.toString().equals(info.getFileSourceType())) {
            String sql = (String) resvMap.get(FileSourceContents.SQL_CONTENT);
            String jdbcUrl = (String) resvMap.get(FileSourceContents.SQL_JDBC_URL);
            String jdbcUser = (String) resvMap.get(FileSourceContents.SQL_JDBC_USER);
            String jdbcPassword = (String) resvMap.get(FileSourceContents.SQL_JDBC_PASSWORD);
            String fileType = (String) resvMap.get(FileSourceContents.FILE_TYPE);
            String fileTemplate = (String) resvMap.get(FileSourceContents.FILE_TEMPLATE);
            Assert.isNull(sql, "参数[" + FileSourceContents.SQL_CONTENT + "]不能为空");
            Assert.isNull(jdbcUrl, "参数[" + FileSourceContents.SQL_JDBC_URL + "]不能为空");
            Assert.isNull(jdbcUser, "参数[" + FileSourceContents.SQL_JDBC_USER + "]不能为空");
            Assert.isNull(jdbcPassword, "参数[" + FileSourceContents.SQL_JDBC_PASSWORD + "]不能为空");
            Assert.isNull(fileType, "参数[" + FileSourceContents.FILE_TYPE + "]不能为空");
            if (FileTypeEnum.DBF.equals(FileSourceContents.FILE_TYPE)) {
                //  DBF 文件必须 要有模板
                Assert.isNull(fileTemplate, "参数[" + FileSourceContents.FILE_TEMPLATE + "]不能为空");
            }

        }
    }
}
